//
//  JXHomeController.m
//  JiaoX
//
//  Created by yinqy on 15/1/5.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "JXHomeController.h"
#import "JXBaseRequest.h"
#import "HomeDataTool.h"
#import "HomeTabInfo.h"
#import "JXScrollBar.h"
#import "CheckLabel.h"
#import "JXReviewListController.h"
#import "HomeFirstPageInfo.h"
#import "JXFirstHomePageController.h"
#import "HomeInfoDao.h"
#import "JXTabBarController.h"

@interface JXHomeController ()<UIScrollViewDelegate,JXScrollBarDelegate>

@property(nonatomic,strong)UIScrollView *scrollView;

@property(nonatomic,strong)NSMutableArray *childController;
@property(nonatomic,strong)NSMutableArray *titleArray;

@property(nonatomic,strong)JXScrollBar *scrollBar;
@property(nonatomic,strong)HomeInfoDao *homeInfoDao;

@end

@implementation JXHomeController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"";
    self.view.backgroundColor = [UIColor whiteColor];
    
    _homeInfoDao = [[HomeInfoDao alloc]init];
    _childController = [NSMutableArray array];
    _titleArray = [NSMutableArray array];
    
    
    UIScrollView *scrollView = [[UIScrollView alloc]init];
    scrollView.frame = CGRectMake(0, 0, self.view.width, self.view.height);
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.pagingEnabled = YES;
    //设置scrollview的代理
    scrollView.delegate = self;
    scrollView.scrollsToTop = NO;
    _scrollView = scrollView;
    
    [self.view addSubview:scrollView];
    JXScrollBar *scrollBar = [[JXScrollBar alloc]initWithFrame:CGRectMake(0, 0, JXScreenW-16, 44) type:JXScrollBarSplitTypeDefault];
    scrollBar.delegate = self;
    _scrollBar = scrollBar;
//    self.navigationItem.titleView = _scrollBar;
    
    [self setData:[_homeInfoDao getFirstPageInfo]];
    
    [self requestData];
    
}

-(void)requestData{
    
    [HomeDataTool getFirstHomePageData:^(HomeFirstPageInfo *result) {
        JXLog(@"%@",result);
        [_homeInfoDao saveFirstPageInfo:result];
        [self setData:result];
    } failure:^(NSError *error) {
        
    }];
}

-(void)setData:(HomeFirstPageInfo *)result{
    if (result==nil) {
        return;
    }
    
    for (UIViewController *ctrl in _childController) {
        [ctrl.view removeFromSuperview];
    }
    [_childController removeAllObjects];
    [_titleArray removeAllObjects];
    
    for (int i=0; i<result.labelList.count; i++){
        CheckLabel *label =result.labelList[i];
        [_titleArray addObject:label.name];
        if(i){
            JXReviewListController *controller = [JXReviewListController initWithCheckLabel:label];
            [_childController addObject:controller];
            
            UIView *view = controller.view;
            view.frame =CGRectMake(i*view.frame.size.width, 0, JXScreenW, self.view.height-113);
            [_scrollView addSubview:view];
        }else{
            JXFirstHomePageController *ctrl = [[JXFirstHomePageController alloc]initWithFirstPageInfo:result];
            [_childController addObject:ctrl];
            
            UIView *view = ctrl.view;
            view.frame =CGRectMake(i*view.frame.size.width, 0, JXScreenW, self.view.height-113);
            [_scrollView addSubview:view];
        }
    }
    [_scrollBar setTitleArray:_titleArray];
    [_scrollBar scrollViewDidScroll:_scrollView];
    //使第1页去加载数据
    [self scrollViewDidEndDecelerating:_scrollView];
    _scrollView.contentSize = CGSizeMake(result.labelList.count*JXScreenW, 0);
    
    if(result.labelList.count<=1){
        [self.navigationItem setTitle:@"首页"];
    }else{
        self.navigationItem.titleView = _scrollBar;
    }
}

#pragma mark -jxscrollbar代理，处理点击title的button事件
- (void)titleClicked:(JXScrollBar *)scrollbar index:(int)index{
    [scrollbar autoScrollToCenter:index];
    [self setScrollCurrentPageNum:index];
}

#pragma mark -滚动代理
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [_scrollBar scrollViewDidScroll:scrollView];
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    //当前页数
    int pageNum = (scrollView.contentOffset.x+scrollView.width*0.5)/scrollView.width;
    
    [self setScrollCurrentPageNum:pageNum];
    [_scrollBar scrollViewDidEndDecelerating:scrollView];
}

-(void)setScrollCurrentPageNum:(int)pageNum{
    CGPoint p = CGPointMake(pageNum*_scrollView.frame.size.width, 0);
    [_scrollView setContentOffset:p];
    //这句代码的作用是解决点击顶部之后底部的scrollview往上跑的问题
    _scrollView.y = 0;
    if ([_childController[pageNum] isKindOfClass:[JXReviewListController class]]) {
        JXReviewListController *controller = _childController[pageNum];
        [controller checkRequestData];
    }
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] postNotificationName:NOTI_UPDATA_BADGE_NUM object:nil];
    
    NSDictionary *userInfo = [UserDefaultsHelper dictionaryForKey:KEY_NOTI_PUSH_STRING];
    if (userInfo) {
        JXNavigationController *navCtl = (JXNavigationController *)self.navigationController;
        [navCtl dealNoti:userInfo isEnterForeground:YES];
        [UserDefaultsHelper setObject:nil forKey:KEY_NOTI_PUSH_STRING];
    }
}

@end
